//
//  RefreshCarLocation.m
//  CarCare
//
//  Created by ileo on 15/4/29.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "RefreshCarLocation.h"
#import "NetCar.h"
#import "AIUserInfo.h"
#import "MapReGeocodeSimple.h"

@interface RefreshCarLocation()

@property (nonatomic , strong) MapReGeocodeSimple * mapSimple;

@end
@implementation RefreshCarLocation

IMP_SINGLETON

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.timeInterval = 15;
         self.mapSimple = [[MapReGeocodeSimple alloc] init];
        __weak __typeof(self) wself = self;
        self.Refresh = ^(){
            [[NetCar locationWithImei:USERINFO.pastDevice] requestSuccess:^(id JSON) {
                CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([JSON[@"lat"] doubleValue], [JSON[@"lng"] doubleValue]);

                if (wself.location.latitude != coordinate.latitude || wself.location.longitude != coordinate.longitude) {
                    wself.location = coordinate;
                    
                    //添加获取城市
                    [wself.mapSimple reGeoCodeCoordinate:wself.location finish:^(NSString *poi, AMapReGeocode *reGeocode) {
                        [MapHelper addressFromReGeocode:reGeocode result:^(NSString *province, NSString *city, NSString *district) {
                            wself.cityNameStr = city;
                        }];
                    } error:^{
                        
                    }];
                    
                    if (wself.RefreshCallBack) {
                        wself.RefreshCallBack(JSON);
                    }
                }
            }];
        };
    }
    return self;
}

@end
